package org.springblade.modules.app.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springblade.common.constant.LndxKTConstants;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.app.dto.LndxCourseDTO;
import org.springblade.modules.app.entity.LndxCourse;
import org.springblade.modules.app.entity.LndxFile;
import org.springblade.modules.app.service.LndxCourseService;
import org.springblade.modules.app.service.LndxFileService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

import static org.springblade.core.cache.constant.CacheConstant.SYS_CACHE;


@RestController
@RequestMapping("blade-app/xfc")
public class LndxCourseXFCController extends BladeController {

    @Autowired
    private LndxCourseService lndxCourseService;

    @Autowired
    private LndxFileService lndxFileService;


    @GetMapping("/pageBySearch")
    public R<IPage<LndxCourseDTO>> pageBySearch(Page page, LndxCourseDTO param) {
        LndxCourseDTO  authparam = new LndxCourseDTO ();
        if (!StringUtils.isEmpty(param.getCategory1())) {
            authparam.setCategory1(param.getCategory1());
        }
        if (!StringUtils.isEmpty(param.getCategory2())) {
            authparam.setCategory2(param.getCategory2());
        }
        if (!StringUtils.isEmpty(param.getCategory3())) {
            authparam.setCategory3(param.getCategory3());
        }
        if (!StringUtils.isEmpty(param.getCoursename())) {
            authparam.setCoursename(param.getCoursename());
        }

        IPage<LndxCourseDTO> pages = lndxCourseService.getLndxPageByWedAdmin(page,authparam);

        for (LndxCourseDTO entity: pages.getRecords()) {
            if(StringUtils.isEmpty(entity.getFiledic())){
                entity.setVideourl(entity.getFilehost()+entity.getFilepath()+"/"+entity.getFilename());
            }else{
                entity.setVideourl(entity.getFilehost()+entity.getFiledic()+"/"+entity.getFilepath()+"/"+entity.getFilename());
            }
        }
        return R.data(pages);
    }


    @GetMapping("/detail")
    public R<LndxCourseDTO> detail(LndxCourseDTO params) {
        LndxCourse detail = lndxCourseService.getOne(Condition.getQueryWrapper(params));
        LndxCourseDTO result = new LndxCourseDTO();
        BeanUtils.copyProperties(detail,result);
        return R.data(result);
    }

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    public R submit(@RequestBody LndxCourse params) {
        return R.status(lndxCourseService.saveOrUpdate(params));
    }

    @PostMapping("/remove")
    public R remove(@RequestParam String ids) {
        return R.status(lndxCourseService.removeByIds(Func.toLongList(ids)));
    }



}
